package com.tongyi.lizhishop.activitys;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import com.allen.library.SuperTextView;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.api.MessageApi;
import com.tongyi.lizhishop.beans.WithDrawRecordBean;
import com.tongyi.lizhishop.common.CommonObserver;
import com.tongyi.lizhishop.common.RecycleViewActivity;
import com.tongyi.lizhishop.constants.Constants;
import com.tongyi.lizhishop.managers.APIServiceManager;
import com.tongyi.lizhishop.utils.AppStringUtils;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class WithDrawRecordActivity extends RecycleViewActivity {

    private CommonAdapter<WithDrawRecordBean.ArrayBean> adapter;
    ArrayList<WithDrawRecordBean.ArrayBean> dataitems = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initTitleBarView(titlebar, "提现记录");
        initView();
        refreshLayout.autoRefresh();
    }

    private void initView() {
        recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new CommonAdapter<WithDrawRecordBean.ArrayBean>(this, R.layout.activity_with_draw_recycleview_item, dataitems) {
            @Override
            protected void convert(ViewHolder holder, WithDrawRecordBean.ArrayBean o, int position) {
                SuperTextView superTextView = (SuperTextView) holder.getView(R.id.super_tv1);
                superTextView.setLeftTopString(o.getBan_money() + "");
                superTextView.setLeftBottomString(AppStringUtils.getSafeTime("yyyy-MM-dd HH:mm:ss", o.getBan_pubtime()) + "");
                superTextView.setRightString(AppStringUtils.getWithDrawStateString(o.getBan_state()));
                int colorValue = o.getBan_state() == 2 ? Color.parseColor("#00f137") : getResources().getColor(R.color.mainColor);
                superTextView.setRightTextColor(colorValue);
            }


        };
        recyclerview.setAdapter(adapter);
    }


    @Override
    public void loadData() {
        APIServiceManager.getInstance().create(MessageApi.class)
                .showBank(SPUtils.getInstance().getString("userid"), Constants.perPageSize, currentPageIndex)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new CommonObserver<WithDrawRecordBean>() {
                    @Override
                    public void onSuccess(WithDrawRecordBean value) {
                        LogUtils.w(value.toString());

                        if (currentPageIndex == 0) {
                            dataitems.clear();
                        }
                        calculatePageIndexAndControlRefreshLayout(value.getArray());
                        if (value.getArray().size() == 0 && currentPageIndex == 0) {
                            multipleStatusView.showEmpty();
                        } else {
                            dataitems.addAll(value.getArray());
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                        checkNetWork();
                    }
                });

    }

}
